using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.WindowsCE.Forms;
using System.IO.Ports;
using System.Collections;

namespace NWT
{
	/// <summary>
	/// Class to setup parameters
	/// </summary>
	public partial class SettingsWindow : Form
	{
		/// <summary>
		/// Configuration to config
		/// </summary>
		protected Configuration configuration = null;

		/// <summary>
		/// Soft input panel
		/// </summary>
		private InputPanel inputPanel = null;

		/// <summary>
		/// Initial form heigh
		/// </summary>
		protected int initialHeight = 0;

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="configuration"></param>
		public SettingsWindow(Configuration configuration)
		{
			this.configuration = configuration;
			InitializeComponent();
			// Add com port names
			AddPortNamesToCombo();

			try
			{
				this.inputPanel = new InputPanel();
				this.inputPanel.EnabledChanged += new System.EventHandler(this.inputPanel_EnabledChanged);
			}
			catch
			{
			}

			chkLogToFile.Focus();
		}

		/// <summary>
		/// Constructor
		/// </summary>
		public SettingsWindow()
		{
			InitializeComponent();
			// Add com port names
			AddPortNamesToCombo();
		}

		/// <summary>
		/// Add com port names
		/// </summary>
		private void AddPortNamesToCombo()
		{
			// Add virtual port
			cmbGPSPort.Items.Add("Virtual");

			// get port names
			string[] ports = SerialPort.GetPortNames();
			// sort ports by name
			ArrayList ar = new ArrayList(ports);
			ar.Sort(new NWT.GPSDevice.ComPortStringComparer());
			ports = (string[])ar.ToArray(typeof(string));
			foreach (string portName in ports)
				cmbGPSPort.Items.Add(portName);
		}

		/// <summary>
		/// Save configuration
		/// </summary>
		protected void SaveConfiguration()
		{
			if (configuration != null)
			{
				// page 1
				configuration.LogToFile = chkLogToFile.Checked;
				configuration.TurnBluetoothOn = chkTurnBlueToothOn.Checked;

				// page 2
				configuration.GPSAccess = cmbGPSPort.SelectedIndex == 0 ? 2 : 1;
				configuration.DeviceParameters.PortName = cmbGPSPort.Text;
				configuration.DeviceParameters.BaudRate = Convert.ToInt32(txtGPSRate.Text);
				configuration.DeviceParameters.TimeOut = Convert.ToInt32(txtGPSTimeout.Text);

				// page 3
				configuration.NetworkParameters.URLPath = txtNetURL.Text;
				configuration.NetworkParameters.MarkerName = txtNetName.Text;
				configuration.NetworkParameters.GameName = txtNetGame.Text;
				configuration.NetworkParameters.UserLogin = txtNetUser.Text;
				configuration.NetworkParameters.UserPassword = txtNetPassword.Text;
				configuration.NetworkParameters.ProxyAddress = txtNetProxyAddress.Text;
				configuration.NetworkParameters.ProxyAddress = txtNetProxyAddress.Text;
				configuration.NetworkParameters.ProxyLogin = txtNetProxyLogin.Text;
				configuration.NetworkParameters.ProxyPassword = txtNetProxyPassword.Text;
				configuration.NetworkParameters.TimeOut = Convert.ToInt32(txtNetTimeout.Text);
				configuration.NetworkParameters.SendingPause = Convert.ToInt32(txtNetSendingPause.Text);
				configuration.NetworkParameters.MaxSendingPause = Convert.ToInt32(txtNetMaxSendingPause.Text);
				configuration.NetworkParameters.DistanceInMeters = Convert.ToInt32(txtNetDistanceInMeters.Text);

				// page 4
				configuration.SMSName = txtSMSName.Text;
				configuration.SMSPhone = txtSMSPhone.Text;
				configuration.SMSShowButton = chkSMSShowButton.Checked;

				// page 5
				configuration.EmailName = txtEmailName.Text;
				configuration.EmailAddress = txtEmail.Text;
				configuration.EmailShowButton = chkEmailShowButton.Checked;

				// page 6
				configuration.TrafficCalculate = chkTrafficCalculate.Checked;
			}
		}

		/// <summary>
		/// Process form loading
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SettingsWindow_Load(object sender, EventArgs e)
		{
			initialHeight = pnlTop.Height;
			if (configuration != null)
			{
				// page 1
				chkLogToFile.Checked = configuration.LogToFile;
				if (configuration.GPSAccess < 1)
					configuration.GPSAccess = 1;
				if (configuration.GPSAccess > 2)
					configuration.GPSAccess = 2;
				chkTurnBlueToothOn.Checked = configuration.TurnBluetoothOn;

				// page 2
				cmbGPSPort.Text = configuration.DeviceParameters.PortName;
				if (configuration.GPSAccess == 2)
					cmbGPSPort.SelectedIndex = 0;
				txtGPSRate.Text = configuration.DeviceParameters.BaudRate.ToString();
				txtGPSTimeout.Text = configuration.DeviceParameters.TimeOut.ToString();

				// page 3
				txtNetURL.Text = configuration.NetworkParameters.URLPath;
				txtNetName.Text = configuration.NetworkParameters.MarkerName;
				txtNetGame.Text = configuration.NetworkParameters.GameName;
				txtNetUser.Text = configuration.NetworkParameters.UserLogin;
				txtNetPassword.Text = configuration.NetworkParameters.UserPassword;
				txtNetProxyAddress.Text = configuration.NetworkParameters.ProxyAddress;
				txtNetProxyPort.Text = configuration.NetworkParameters.ProxyPort.ToString();
				txtNetProxyLogin.Text = configuration.NetworkParameters.ProxyLogin;
				txtNetProxyPassword.Text = configuration.NetworkParameters.ProxyPassword;
				txtNetTimeout.Text = configuration.NetworkParameters.TimeOut.ToString();
				txtNetSendingPause.Text = configuration.NetworkParameters.SendingPause.ToString();
				txtNetMaxSendingPause.Text = configuration.NetworkParameters.MaxSendingPause.ToString();
				txtNetDistanceInMeters.Text = configuration.NetworkParameters.DistanceInMeters.ToString();

				// page 4
				txtSMSName.Text = configuration.SMSName;
				txtSMSPhone.Text = configuration.SMSPhone;
				chkSMSShowButton.Checked = configuration.SMSShowButton;

				// page 5
				txtEmailName.Text = configuration.EmailName;
				txtEmail.Text = configuration.EmailAddress;
				chkEmailShowButton.Checked = configuration.EmailShowButton;

				// page 6
				chkTrafficCalculate.Checked = configuration.TrafficCalculate;
			}
		}

		/// <summary>
		/// Perform button event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void mniSave_Click(object sender, EventArgs e)
		{
			try
			{
				this.DialogResult = DialogResult.OK;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				this.DialogResult = DialogResult.None;
			}
		}

		/// <summary>
		/// Perform button event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void mniCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
		}

		/// <summary>
		/// Process panel event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void inputPanel_EnabledChanged(object sender, EventArgs e)
		{
			if (inputPanel != null)
			{
				if (inputPanel.Enabled)
				{
					Size pnlTopSize = pnlTop.Size;
					pnlTop.Dock = DockStyle.None;
					pnlTop.Size = new Size(pnlTopSize.Width, pnlTopSize.Height - inputPanel.Bounds.Height);
				}
				else
				{
					pnlTop.Dock = DockStyle.Fill;
				}
			}
		}

		/// <summary>
		/// Proecess event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SettingsWindow_Closed(object sender, EventArgs e)
		{
			if (inputPanel != null)
			{
				// detach from softpanel event
				this.inputPanel.EnabledChanged -= new System.EventHandler(this.inputPanel_EnabledChanged);
			}

			if (DialogResult == DialogResult.OK)
			{
				SaveConfiguration();
			}
		}

		/// <summary>
		/// Choose contact for SMS
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnChooseContactForSMS_Click(object sender, EventArgs e)
		{
			try
			{
				using (ChooseContactDialog contactDialog = new ChooseContactDialog())
				{
					contactDialog.ChooseContactOnly = false;
					contactDialog.RequiredProperties = new ContactProperty[] { ContactProperty.AllPhone };
					DialogResult result = contactDialog.ShowDialog();
					if (result == DialogResult.OK)
					{
						txtSMSName.Text = contactDialog.SelectedContactName;
						txtSMSPhone.Text = contactDialog.SelectedPropertyValue;
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show (ex.Message);
			}
		}

		/// <summary>
		/// Choose contact for Email
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnChooseContactForEmail_Click(object sender, EventArgs e)
		{
			try
			{
				using (ChooseContactDialog contactDialog = new ChooseContactDialog())
				{
					contactDialog.ChooseContactOnly = false;
					contactDialog.RequiredProperties = new ContactProperty[] { ContactProperty.AllEmail };
					DialogResult result = contactDialog.ShowDialog();
					if (result == DialogResult.OK)
					{
						txtEmailName.Text = contactDialog.SelectedContactName;
						txtEmail.Text = contactDialog.SelectedPropertyValue;
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		/// <summary>
		/// Process event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Control_GotFocus(object sender, EventArgs e)
		{
			//if (inputPanel != null)
			//    inputPanel.Enabled = true;
		}

		/// <summary>
		/// Process event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Control_LostFocus(object sender, EventArgs e)
		{
			//if (inputPanel != null)
			//    inputPanel.Enabled = false;
		}

		/// <summary>
		/// Reset traffic
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnClearTraffic_Click(object sender, EventArgs e)
		{
			GPSManager.NetworkTrafficCounter.Reset();
		}

		/// <summary>
		/// Process port changing
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void cmbGPSPort_TextChanged(object sender, EventArgs e)
		{
			if (cmbGPSPort.Text == "Virtual")
			{
				if (txtGPSRate.Enabled)
					txtGPSRate.Enabled = false;
				if (txtGPSTimeout.Enabled)
					txtGPSTimeout.Enabled = false;
			}
			else
			{
				if (!txtGPSRate.Enabled)
					txtGPSRate.Enabled = true;
				if (!txtGPSTimeout.Enabled)
					txtGPSTimeout.Enabled = true;
			}
		}
	}
}